package com.jfinal.ext.generator;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

public class MenuGenerator {
    protected String first = "<aside class=\"main-sidebar\">\n" +
            "    <section class=\"sidebar\">\n" +
            "        <ul class=\"sidebar-menu\">\n" +
            "            <li class=\"header\">MAIN NAVIGATION</li>\n" +
            "            <li class=\"treeview <#if ";
    protected String fifth = "menu=='/%s'||";
    protected String second = ">active</#if>\">\n" +
            "                <a href=\"#\">\n" +
            "                    <i class=\"fa fa-dashboard\"></i> <span>信息</span> <i class=\"fa fa-angle-left pull-right\"></i>\n" +
            "                </a>\n" +
            "                <ul class=\"treeview-menu\">\n";
    protected String third = "                    <li class=\"<#if menu=='/%s'>active</#if>\"><a href=\"/%s\"><i class=\"fa fa-circle-o\"></i>%s</a>\n"+
            "                    </li>\n";

    protected String fourth = "                </ul>\n" +
            "            </li>\n" +
            "        </ul>\n" +
            "    </section>\n" +
            "</aside>";
    protected String sixth = "        </ul>\n" +
            "    </section>\n" +
            "</aside>";
    protected String seventh = "<aside class=\"main-sidebar\">\n" +
            "    <section class=\"sidebar\">\n" +
            "        <ul class=\"sidebar-menu\">\n" +
            "            <li class=\"header\">MAIN NAVIGATION</li>\n";
    protected String eighth = "            <li class=\"<#if menu=='/%s'>active</#if>\">\n" +
            "                <a href=\"/%s\"> <i class=\"fa fa-dashboard\"></i> <span>%s</span>\n" +
            "                </a>\n" +
            "            </li>\n";
    protected String MenuOutputDir;
    protected boolean hasChildNode;

    public MenuGenerator(String MenuOutputDir, boolean haschildnode) {
        super();
        this.MenuOutputDir = MenuOutputDir;
        this.hasChildNode = haschildnode;
    }

    public void generate(List<TableMeta> tableMetas) {
        if (hasChildNode) {
            System.out.println("Generate Menu haschildnode Pages ...");
            StringBuilder res = new StringBuilder();
            try {
                genHasChildNodeContent(tableMetas, res);
                writeToFile(res, "menuHasChildNode.html");
            } catch (IOException e) {
                // TODO 异常处理块
                e.printStackTrace();
            }
        } else {
            System.out.println("Generate Menu hasNochildnode Pages ...");
            StringBuilder res = new StringBuilder();
            try {
                genNoChildNodeContent(tableMetas, res);
                writeToFile(res, "menuNoChildNode.html");

            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    private void genNoChildNodeContent(List<TableMeta> tableMetas, StringBuilder res) {
        res.append(seventh);
        genEighth(tableMetas, res);
        res.append(sixth);

    }

    private void genEighth(List<TableMeta> tableMetas, StringBuilder res) {
        for (TableMeta tableMeta : tableMetas) {
            String s = tableMeta.modelName.toLowerCase().replaceAll("_", "");
            res.append(String.format(eighth, s, s, tableMeta.remarks));
        }

    }

    public void genHasChildNodeContent(List<TableMeta> tableMetas, StringBuilder res) throws IOException {
        res.append(first);
        genFifth(tableMetas, res);
        res.append(second);
        genThird(tableMetas, res);
        res.append(fourth);

    }

    public void genFifth(List<TableMeta> tableMetas, StringBuilder res) {
        for (int i = 0; i < tableMetas.size(); i++) {
            String s = tableMetas.get(i).modelName.toLowerCase().replaceAll("_", "");
            if (i == tableMetas.size() - 1) {
                res.append("menu=='/" + s + "'");
            } else {
                res.append(String.format(fifth, s));
            }
        }

    }

    public void genThird(List<TableMeta> tableMetas, StringBuilder res) {
        for (TableMeta tableMeta : tableMetas) {
            String s = tableMeta.modelName.toLowerCase().replaceAll("_", "");
            res.append(String.format(third, s, s, tableMeta.remarks));
        }

    }

    public void writeToFile(StringBuilder res, String fileName) throws IOException {
        File dir = new File(MenuOutputDir + File.separator);
        if (!dir.exists())
            dir.mkdirs();
        String target = MenuOutputDir + File.separator + fileName;
        File file = new File(target);
        if (file.exists()) {
            return;
        }

        FileWriter fw = null;
        try {
            fw = new FileWriter(file);
            fw.write(res.toString());

        }

        finally {
            fw.close();
        }

    }

}
